import { useUserStore } from '@/stores/user'
import {
  executeDeleteExerciseDislike,
  executeDeleteFoodDislike,
  executeUpsertExerciseDislike,
  executeUpsertFoodDislike,
  queryExerciseDislike,
  queryFoodDislike,
} from '@/AGC/manager/dislike'
import { getFoodIdsByNameUtil } from '@/AGC/utils/food'
import { FoodDislike } from '@/AGC/model/FoodDislike'
import { queryExerciseListByName } from '@/AGC/manager/exercise'
import { ExerciseDislike } from '@/AGC/model/ExerciseDislike'

/**
 * 获取用户不喜欢的运动 ids
 * @returns 用户不喜欢的运动 ids
 */
export const getExerciseDislikeIdsUtil = async () => {
  const user_id = useUserStore().userInfo.id
  const exerciseDislikeList = await queryExerciseDislike(user_id)
  return exerciseDislikeList.map((item) => item.getExercise_id())
}

/**
 * 获取用户不喜欢的食物 ids
 * @returns 用户不喜欢的食物 ids
 */
export const getFoodDislikeIdsUtil = async () => {
  const user_id = useUserStore().userInfo.id
  const foodDislike = await queryFoodDislike(user_id)
  return foodDislike.map((item) => item.getFood_id())
}

/**
 * 创建用户不喜欢的食物
 * @param foodName 食物名称
 */
export const createFoodDislikeUtil = async (foodName: string) => {
  const foodIds = await getFoodIdsByNameUtil(foodName)

  if (!foodIds) return

  const foodDislike = new FoodDislike()
  foodDislike.setUser_id(useUserStore().userInfo.id)
  foodDislike.setFood_id(foodIds[0])
  await executeUpsertFoodDislike(foodDislike)
}

/**
 * 创建用户不喜欢的运动
 * @param exerciseName 运动名称
 */
export const createExerciseDislikeUtil = async (exerciseName: string) => {
  const exerciseList = await queryExerciseListByName(exerciseName)
  const exerciseIds = exerciseList.map((item) => item.getId()!)

  const exerciseDislike = new ExerciseDislike()
  exerciseDislike.setUser_id(useUserStore().userInfo.id)
  exerciseDislike.setExercise_id(exerciseIds[0])
  await executeUpsertExerciseDislike(exerciseDislike)
}

/**
 * 清除食物的历史缓存
 */
export const clearFoodHistoryCacheUtil = async () => {
  const user_id = useUserStore().userInfo.id
  const foodDislikeList = await queryFoodDislike(user_id)
  await executeDeleteFoodDislike(foodDislikeList)
}

/**
 * 清除运动的历史缓存
 */
export const clearExerciseHistoryCacheUtil = async () => {
  const user_id = useUserStore().userInfo.id
  const exerciseDislikeList = await queryExerciseDislike(user_id)
  await executeDeleteExerciseDislike(exerciseDislikeList)
}
